package com.js.myapp.system.activity;

// Created by JS on 2021/2/5.

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.js.baseview.selector.ListPopupWindow;
import com.js.baseview.selector.SearchListPopupWindow;
import com.js.baseview.selector.adapter.ListDropDownAdapter;
import com.js.baseview.selector.adapter.SelectionAdapter;
import com.js.baseview.selector.entity.SelectionItem;
import com.js.basicpage.activity.BaseLoadingDialogActivity;
import com.js.basicutils.DialogUtil;
import com.js.basicutils.NavigationBarUtil;
import com.js.basicutils.StatusBarUtil;
import com.js.basicutils.ToastUtils;
import com.js.basicutils.Utils;
import com.js.myapp.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CommonWidgetActivity extends BaseLoadingDialogActivity {

    @BindView(R.id.btn_dialog)
    Button btnDialog;
    @BindView(R.id.btn_toast)
    Button btnToast;
    @BindView(R.id.btn_down_select)
    Button btnDownSelect;
    @BindView(R.id.btn_down_search_select)
    Button btnDownSearchSelect;
    @BindView(R.id.et_weather)
    EditText etWeather;
    @BindView(R.id.et_soil_type)
    EditText etSoilType;
    @BindView(R.id.btn_checked_item_selector)
    Button btnCheckedItemSelector;
    @BindView(R.id.et_checked_item)
    EditText etCheckedItem;
    @BindView(R.id.btn_refresh_load_more)
    Button btnRefreshLoadMore;
    @BindView(R.id.btn_shape_img)
    Button btnShapeImg;

    private CommonWidgetActivity mContext;

    private SelectionAdapter weatherAdapter;
    private ListPopupWindow lpWeather;
    private String[] weathers = {"风", "云", "雾", "雨", "闪", "雪", "霜", "雷", "雹", "霾"};
    private List<SelectionItem> mWeatherTypes = new ArrayList<>();

    private SearchListPopupWindow lpSoilType;
    private List<SelectionItem> mSoilTypes = new ArrayList<>();
    private SelectionItem mSelectedSoilType;

    private ListPopupWindow lpCheckItem;
    private ListDropDownAdapter checkItemAdapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, CommonWidgetActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_widget);
        ButterKnife.bind(this);
        mContext = this;
        NavigationBarUtil.setSysNavigationColor(this, Color.parseColor("#141d26"));
        StatusBarUtil.setStatusBarColor(this, R.color.color_statusbar_bg);
    }

    @OnClick({R.id.btn_dialog, R.id.btn_toast, R.id.btn_down_select, R.id.et_weather, R.id.btn_down_search_select,
            R.id.et_soil_type, R.id.btn_checked_item_selector, R.id.et_checked_item, R.id.btn_refresh_load_more,
            R.id.btn_shape_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_dialog:
                showTipDialog();
                break;
            case R.id.btn_toast:
                ToastUtils.showToast(mContext, "这是用来测试的提示！");
                break;
            case R.id.btn_down_select:
            case R.id.et_weather:
                showDownSelector();
                break;
            case R.id.btn_down_search_select:
            case R.id.et_soil_type:
                showDownSearchSelector();
                break;
            case R.id.btn_checked_item_selector:
            case R.id.et_checked_item:
                showCheckedItemSelector();
                break;
            case R.id.btn_refresh_load_more:
                RefreshLoadMoreActivity.start(mContext);
                break;
            case R.id.btn_shape_img:
                ShapeImageActivity.start(mContext);
                break;
        }
    }

    /**
     * 显示提示框
     */
    private void showTipDialog() {
        DialogUtil.showTipDialog(mContext, "提示", "这是一个测试提示", "好的", new DialogUtil.DialogListener() {
            @Override
            public void onConfirm() {
                ToastUtils.showToast(mContext, "点击了确定！");
            }

            @Override
            public void onCancel() {
                ToastUtils.showToast(mContext, "点击了取消！");
            }
        });
    }

    /**
     * 风、云、雾、雨、闪、雪、霜、雷、雹、霾
     * 显示下拉框
     */
    private void showDownSelector() {
        if (mWeatherTypes.size() == 0) {
            for (int i = 0; i < weathers.length; i++) {
                SelectionItem selectionItem = new SelectionItem();
                selectionItem.setId(i + "");
                selectionItem.setCode("weather" + i);
                selectionItem.setData(weathers[i]);
                mWeatherTypes.add(selectionItem);
            }
        }
        if (weatherAdapter == null) {
            weatherAdapter = new SelectionAdapter(mContext, mWeatherTypes);
        } else {
            weatherAdapter.notifyDataSetChanged();
        }
        if (lpWeather != null && lpWeather.isShowing()) {
            lpWeather.disMissPopup();
        } else {
            if (lpWeather == null) {
                lpWeather = new ListPopupWindow();
                lpWeather.initPopWindow(this,
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                );
                lpWeather.setAdapter(weatherAdapter);
                lpWeather.setOnListItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        SelectionItem selectionItem = mWeatherTypes.get(position);
                        etWeather.setText(selectionItem.getData());
                        lpWeather.disMissPopup();
                    }
                });
            }
            lpWeather.showAsDropDown(etWeather);
        }
    }

    /**
     * 显示带搜索的下拉框
     */
    private void showDownSearchSelector() {
        clearFocus();
        if (mSoilTypes.size() == 0) {
            String[] soilTypeArray = getResources().getStringArray(R.array.soil_type);
            for (int i = 0; i < soilTypeArray.length; i++) {
                SelectionItem selectionItem = new SelectionItem();
                selectionItem.setId(i + "");
                selectionItem.setData(soilTypeArray[i]);
                selectionItem.setCode("soilType" + i);
                mSoilTypes.add(selectionItem);
            }
        }
        if (lpSoilType != null && lpSoilType.isShowing()) {
            lpSoilType.disMissPopup();
        } else {
            if (lpSoilType == null) {
                lpSoilType = new SearchListPopupWindow();
                lpSoilType.initPopWindow(this,
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        mSoilTypes,
                        "请输入土壤类型关键字"
                );
                lpSoilType.setOnListItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        List<SelectionItem> showingItems = lpSoilType.getShowingItems();
                        mSelectedSoilType = showingItems.get(position);
                        etSoilType.setText(mSelectedSoilType.getData());
                        lpSoilType.disMissPopup();
                    }
                });
            }
            lpSoilType.showAsDropDown(etSoilType);
        }
    }

    /**
     * 取消当前焦点
     */
    private void clearFocus() {
        View currentFocus = getWindow().getCurrentFocus();
        if (currentFocus != null) {
            Utils.hideInput(this);
            currentFocus.clearFocus();
        }
    }

    /**
     * 显示带选中项的下拉框
     */
    private void showCheckedItemSelector() {
        if (mWeatherTypes.size() == 0) {
            for (int i = 0; i < weathers.length; i++) {
                SelectionItem selectionItem = new SelectionItem();
                selectionItem.setId(i + "");
                selectionItem.setCode("weather" + i);
                selectionItem.setData(weathers[i]);
                mWeatherTypes.add(selectionItem);
            }
        }
        if (checkItemAdapter == null) {
            checkItemAdapter = new ListDropDownAdapter(mContext, mWeatherTypes);
        } else {
            checkItemAdapter.notifyDataSetChanged();
        }
        if (lpCheckItem != null && lpCheckItem.isShowing()) {
            lpCheckItem.disMissPopup();
        } else {
            if (lpCheckItem == null) {
                lpCheckItem = new ListPopupWindow();
                lpCheckItem.initPopWindow(this,
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                );
                lpCheckItem.setAdapter(checkItemAdapter);
                lpCheckItem.setOnListItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        SelectionItem selectionItem = mWeatherTypes.get(position);
                        checkItemAdapter.setCheckItem(position);
                        etCheckedItem.setText(selectionItem.getData());
                    }
                });
            }
            lpCheckItem.showAsDropDown(etCheckedItem);
        }
    }
}
